from myProfile.models.data.myProfile_models import Login, Intervetion
from typing import List


class IntervetionRepository:

    def __init__(self, engine):
        self.engine = engine

    async def add_to_profile(
            self, login_id: int, intervention: Intervetion) -> bool:
        try:
            login = await self.engine.find_one(Login, Login.id == login_id)
            interventions = login.profiles.interventions + [intervention]
            login.profiles.interventions = interventions
            await self.engine.save(login)
        except Exception as e:
            print(e)
            return False
        return True

    async def remove_from_profile(self, login_id: int, id: int) -> bool:
        try:
            login = await self.engine.find_one(Login, Login.id == login_id)
            interventions = login.profiles.interventions
            values = [value for value in interventions if value.id != id]
            login.profiles.interventions = values
            await self.engine.save(login)
        except Exception as e:
            print(e)
            return False
        return True

    async def get_from_profile(self, login_id: int) -> List[Intervetion]:
        login = await self.engine.find_one(Login, Login.id == login_id)
        return login.profiles.interventions
